import ArrayUtil from '@/ArrayUtil'
import AttributeUtil from '@/AttributeUtil'
import RandomUtil from '@/RandomUtil'
import RandomCommon from 'code/Core/Common/RandomCommon'
import { d_cus_items } from 'xlsx/cus_items'
import { d_shop_items } from 'xlsx/shop_items'

const increaseRate = 0.35

const qualityColors = [
    '',
    '|cffffffff',
    '|cff01c901',
    '|cff0971e7',
    '|cffee10d0',
    '|cffe99801',
    '|cffee0034',
]

const qualityNames = ['', '普通', '优秀', '精良', '史诗', '传说', '神话']

const attributeKeys: { [key: string]: string } = {
    attack: '攻击',
    attack_p: '攻击百分比',
    life: '生命',
    life_p: '生命百分比',
    mana: '魔法',
    mana_p: '魔法百分比',
    full_property: '全属性',
    full_property_p: '全属性百分比',
    strength: '体质',
    strength_p: '体质百分比',
    agility: '力量',
    agility_p: '力量百分比',
    intelligence: '精神',
    intelligence_p: '精神百分比',
    attackSpd_p: '攻速',
    move_speed: '移动速度',
    damage_cool: '攻击间隔',
    damage_range: '攻击范围',
    自定义敏捷: '敏捷',
    自定义敏捷百分比: '敏捷百分比',
    自定义智力: '智力',
    自定义智力百分比: '智力百分比',
    命中: '命中',
    附加护甲: '护甲',
    附加魔抗: '魔抗',
    护甲百分比: '护甲百分比',
    魔抗百分比: '魔抗百分比',
    法术强度: '法术强度',
    法术强度百分比: '法术强度百分比',
    物理暴击机率: '物理暴击机率',
    物理暴击伤害: '物理暴击伤害',
    物理伤害倍率: '物理伤害倍率',
    物理伤害减免: '物理伤害减免',
    法术暴击机率: '法术暴击机率',
    法术暴击伤害: '法术暴击伤害',
    法术伤害倍率: '法术伤害倍率',
    法术伤害减免: '法术伤害减免',
    全伤害倍率: '全伤害倍率',
    全伤害减免: '全伤害减免',
    物理伤害穿透: '物理伤害穿透',
    物理伤害穿透百分比: '物理伤害穿透百分比',
    法术伤害穿透: '法术伤害穿透',
    法术伤害穿透百分比: '法术伤害穿透百分比',
    生命恢复: '生命恢复',
    生命恢复增强: '生命恢复增强',
    法力恢复: '法力恢复',
    法力恢复增强: '法力恢复增强',
    治疗增强: '治疗增强',
    受到治疗增强: '受到治疗增强',
    精英伤害增强: '精英伤害增强',
    精英伤害减免: '精英伤害减免',
    物理吸血: '物理吸血',
    法术吸血: '法术吸血',
    分裂伤害系数: '分裂伤害系数',
    分裂范围系数: '分裂范围系数',
    护甲穿透: '护甲穿透',
    护甲穿透百分比: '护甲穿透百分比',
    法术穿透: '法术穿透',
    法术穿透百分比: '法术穿透百分比',
    反射伤害倍率: '反射伤害倍率',
    宠物生命: '宠物生命',
    宠物生命百分比: '宠物生命百分比',
    持续伤害增强: '持续伤害增强',
    持续治疗增强: '持续治疗增强',
    ranged_damage_range: '远程伤害增强',
    melee_damage_range: '近战伤害增强',
}

const multipleAttrKeys = ['life', 'mana']

export default class EquipmentQuality {
    constructor() {}
    static Util = {
        getQualityName(item: item) {
            const attr = AttributeUtil.getItemAttribute(item)
            if (attr?.quality) {
                const q = attr.quality as number
                return `${qualityColors[q]}${GetItemName(item)}(${
                    qualityNames[q]
                })|r`
            } else {
                return `${qualityColors[1]}${GetItemName(item)}(${
                    qualityNames[1]
                })|r`
            }
        },
        getQualityNameByAttribute(name: string, attr: any) {
            attr = attr || {}
            const q = (attr.quality as number) ?? 1
            return `${qualityColors[q]}${name}(${qualityNames[q]})|r`
        },
        getQualityDescription(item: item) {
            const attr = AttributeUtil.getItemAttribute(item)
            const typeAttr = AttributeUtil.getItemTypeAttribute(
                id2string(GetItemTypeId(item))
            )
            const sum = AttributeUtil.sumAttributes([attr, typeAttr])
            return this.getQualityDescriptionByAttribute(sum)
        },
        getQualityDescriptionByAttribute(attr: any) {
            const des: string[] = []
            const keys: string[] = Object.keys(attr)
            for (let key of keys) {
                if (attributeKeys[key]) {
                    const v = attr[key] as number
                    if (v) {
                        const kv = attributeKeys[key] ?? ''
                        // 百分比处理
                        if (
                            kv.includes('百分比') ||
                            kv.includes('暴击') ||
                            kv.includes('倍率') ||
                            kv.includes('增强') ||
                            kv.includes('减免') ||
                            kv.includes('命中') ||
                            kv.includes('吸血')
                        ) {
                            des.push(
                                `${attributeKeys[key]}: ${
                                    v > 0 ? '+' : '-'
                                }${Math.floor(v * 100)}%`
                            )
                        } else if (kv.includes('攻速')) {
                            des.push(
                                `${attributeKeys[key]}: ${
                                    v > 0 ? '+' : '-'
                                }${Math.floor(v * 100)}`
                            )
                        } else {
                            des.push(
                                `${attributeKeys[key]}: ${
                                    v > 0 ? '+' : '-'
                                }${v}`
                            )
                        }
                    }
                }
            }
            return des.length > 0
                ? qualityColors[2] + des.join('|n') + '|r'
                : ''
        },
        getQualityDescForBackPack(id: string, attr: any) {
            const sum = {}
            AttributeUtil.add(sum, AttributeUtil.getItemTypeAttribute(id))
            AttributeUtil.add(sum, attr)
            return this.getQualityDescriptionByAttribute(sum)
        },
        randomItemQuality(item: item) {
            const quality = RandomCommon.randomIntByWeight(1, 5)
            this.setItemQuality(item, quality)
        },
        getQualityAttribute(id: string) {
            for (let it of d_shop_items) {
                if (it.id == id) {
                    return it.QuaAttr
                }
            }
            for (let it of d_cus_items) {
                if (it.id == id) {
                    return it.QuaAttr
                }
            }
            return {}
        },
        setItemQuality(item: item, quality: number) {
            const attr = AttributeUtil.getItemAttribute(item, true)
            quality = Math.max(1, Math.min(6, quality))
            attr.quality = quality
            if (quality <= 1) return
            const quaAttr = this.getQualityAttribute(
                id2string(GetItemTypeId(item))
            )
            const keys: string[] = Object.keys(quaAttr)
            if (keys.length <= 0) return
            const max = RandomCommon.randomIntByWeight(1, keys.length)

            const at = {}
            for (let i = 1; i <= max; i++) {
                const key = ArrayUtil.randomElement(keys)
                // 取对应的 key 的基础值
                const base = quaAttr[key]
                ArrayUtil.removeElement(keys, key)
                // const valueMax = Math.pow(3, quality)
                // const valueMin = Math.pow(3, quality - 1) + 1
                // let value = RandomUtil.nextInt(valueMin, valueMax)
                // 百分比处理
                const kv = attributeKeys?.[key] ?? ''
                let val = 0
                if (
                    kv.includes('百分比') ||
                    kv.includes('暴击') ||
                    kv.includes('倍率') ||
                    kv.includes('增强') ||
                    kv.includes('减免') ||
                    kv.includes('命中') ||
                    kv.includes('吸血') ||
                    kv.includes('攻速')
                ) {
                    const minV = R2I(
                        (base * (quality - 2) * increaseRate + base) * 100 + 1
                    )
                    const maxV = R2I(
                        (base * (quality - 1) * increaseRate + base) * 100
                    )
                    val = RandomUtil.nextInt(minV, maxV) / 100
                } else {
                    const minV = R2I(
                        base * (quality - 2) * increaseRate + base + 1
                    )
                    const maxV = R2I(base * (quality - 1) * increaseRate + base)
                    val = RandomUtil.nextInt(minV, maxV)
                    // if (multipleAttrKeys.includes(key)) {
                    //     // 生命魔法 x 10
                    //     val *= 10
                    // }
                }
                AttributeUtil.add(at, { [key]: val })
            }
            AttributeUtil.add(attr, at)
        },
    }
}
